import { SOUND_SERVER } from '@/common/constant'
import url from '@/common/http.url'

const http = uni.$u.http

// 接口
let api = {
  // 歌单推荐-limit
  getPlaylistRecommend: (limit = 30) => {
    return http.get(url.playlistRecommend, { limit })
  },
  // 歌单分类-limit
  getPlaylistCategory: (type, limit = 60) => {
    return http.get(url.playlistCategory, { type, limit })
  },
  // 歌曲、歌词、歌曲音源-{ id, type, server }
  getMeting: (id, type = 'playlist', server = SOUND_SERVER.wyy) => {
    return http.get(url.musicMeting, {
      server,
      type,
      id,
    })
  },
  // 搜索歌曲、歌手、歌单、关联项-type: 类型  keywords: 关键词  pn: 页数
  getSearch: (type = 'song', keywords, pn = 1) => {
    let address = url.searchSong
    let data = type === 'song' ? { keywords, pn } : { keywords }

    if (type === 'song') address = url.searchSong
    else if (type === 'playlist') address = url.searchPlaylist
    else if (type === 'suggest') address = url.searchSuggest

    return http.get(address, data)
  },
}

// #ifdef MP
// 接口
api = {
  // 歌单推荐-limit
  getPlaylistRecommend: (type = 'recommend', src = 'wyy') => {
    return http.get(url.playlistRecommend, { type, src })
  },
  // 歌单分类-limit
  getPlaylistCategory: (type = 'recommend', src = 'wyy') => {
    return http.get(url.playlistCategory, { type, src })
  },
  // 歌词、歌曲音源-rid
  getMeting: (id, type = 'playlist') => {
    let address = url.wyyList
    let data = type === 'playlist' ? { id: id } : { rid: id }

    if (type === 'playlist') address = url.wyyList
    else if (type === 'url') address = url.wyyMp3
    else if (type === 'lrc') address = url.wyyLrc

    return http.get(address, data)
  },
  // 搜索歌曲、歌手、歌单-type: 类型  key: 关键词  pn: 页数
  getSearch: (type = 'song', key, pn = 1) => {
    let address = url.searchSong
    let data = type === 'song' ? { key, pn } : { key }

    if (type === 'song') address = url.searchSong
    else if (type === 'playlist') address = url.searchPlaylist

    return http.get(address, data)
  },
}
// #endif

export const {
  getPlaylistRecommend,
  getPlaylistCategory,
  getMeting,
  getSearch,
} = api
